﻿using System;
using System.Net;
using System.Windows;
using System.Windows.Controls;
using System.Windows.Documents;
using System.Windows.Ink;
using System.Windows.Input;
using System.Windows.Media;
using System.Windows.Media.Animation;
using System.Windows.Shapes;
using System.Runtime.Serialization;
using System.Threading.Tasks;
using System.Collections.Generic;

namespace RATSharpW7.ViewModel
{
    /// <summary>
    /// Generic data Model view. Defines actions for REST layer and how to extract data.
    /// Provides single DataSource for view.
    /// </summary>
    /// <typeparam name="T">Single object which shoud be loaded</typeparam>
    /// <typeparam name="TT">Datacontainer for object. As almost every communication
    /// is handled in containers (e.g. Reservations -> Reservation) this typu must be also
    /// specified</typeparam>
    [DataContract]
    public class SingleObjectViewModel<T, TT> : ViewModel where TT : List<T>
    {
        [DataMember]
        public T data { get; set; }

        public SingleObjectViewModel()
        {

        }

        public SingleObjectViewModel(T data)
        {
            this.data = data;
        }

        /// <summary>
        /// Bindable datasource.
        /// </summary>
        [DataMember]
        public T DataSource
        {
            get
            {
                return this.data;
            }
            set
            {
                if (!value.Equals(this.data))
                {
                    this.data = value;
                    RaisePropertyChanged("DataSource");
                }
            }
        }

        /// <summary>
        /// Basicaly defines how to notify UI
        /// </summary>
        /// <param name="task">Rest task</param>
        protected void notifyChangeModel(Task<TT> task)
        {
            task.ContinueWith(t =>
            {
                Deployment.Current.Dispatcher.BeginInvoke(() =>
                {
                    if (t.Result != null && t.Result.Count >= 1)
                    {
                        DataSource = t.Result[0];
                    }
                });
            });
    }
    }
}
